package merlionerpclient;
/***********************************************
 * Purposely Made for demo --> 'INIT' 
 **********************************************/

import Merlion.CRM.bean.SalesLeadModuleRemote;
import Merlion.CRM.bean.SalesOrderProcessingModuleRemote;
import Merlion.FRM.bean.CreditProfileBeanRemote;
import Merlion.FRM.bean.InvoiceAndReceiptBeanRemote;
import Merlion.userManagement.bean.RegistrationBeanRemote;
import Merlion.SCM.bean.*;
import Merlion.MRP.bean.*;
import Merlion.SCM.entity.MaterialEntity;
import Merlion.SCM.entity.ProductEntity;
import Merlion.userManagement.bean.LoginBeanRemote;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.ejb.EJB;
import Merlion.util.state.InvoiceAndReceiptState;
import Merlion.util.state.BOMEntryState;
import Merlion.util.state.BillOfMaterialState;
import Merlion.util.state.ClientState;
import Merlion.util.state.IndividualWeeklyRequirementState;
import Merlion.util.state.InventoryState;
import Merlion.util.state.MRPRecordState;
import Merlion.util.state.SalesRecordState;
import Merlion.util.state.PlannedDemandState;
import Merlion.util.state.SOPBreakdownState;
import Merlion.util.state.SalesOperationPlanState;
import Merlion.util.state.WarehouseState;
import Merlion.util.state.WeeklyRequirementBreakdownState;

public class Main {

    @EJB
    private static CreditProfileBeanRemote cpb;
    @EJB
    private static SalesLeadModuleRemote smb;
    @EJB
    private static SalesOrderProcessingModuleRemote sop;
    @EJB
    private static InventoryItemsBeanRemote iib;
    @EJB
    private static InvoiceAndReceiptBeanRemote irb;
    @EJB
    private static DemandManagementBeanRemote dmb;
    @EJB
    private static MaterialManagementBeanRemote mmb;
    @EJB
    private static ProductionManagementBeanRemote pmb;
    @EJB
    private static RegistrationBeanRemote rb;
    @EJB
    private static InventoryMovementsManagementBeanRemote imm;
    @EJB
    private static LoginBeanRemote lb;
    public Scanner scanner = new Scanner(System.in);

    public Main() {
    }

    public static void main(String[] args) {
        Main client = new Main();
        client.doApp();
    }

    public void doApp() {

        try {
            String choice = "";

            while (!choice.equals("0")) {
                System.out.println("***********WELCOME TO MERLION ERP*******");
                System.out.println("");
                System.out.println("INIT.  Initialization for Demo");
                System.out.println("0.  EXIT");

                System.out.print("\nEnter choice : ");
                choice = scanner.nextLine();
                if (choice.equals("INIT")) {
                    initializeUser();
                    initializeSalesLead();
                    initializeCustomers();
                    initializeMaterials();
                    initializeProducts();
                    initializeWarehouses();
                    initializeBillOfMaterial1();
                    initializeBillOfMaterial2();
                    initializeBillOfMaterial3();
                    initializeBillOfMaterial4();
                    initializeMonthlySalesRecords();
                    InitializeSalesForecast();
                    initializeMonthlyPlannedDemands();
                    initializeMaterialRequirements();
                    initializeInventoryBatches();
                } else if (choice.equals("0")) {
                    return;
                }
            }//end while
            return;
        } catch (Exception ex) {
            System.err.println("Caught an unexpected exception!");
            ex.printStackTrace();
        }
    }

    public void initializeMaterials() {
        try {
            System.out.println("Initializing Materials...");
            iib.createMaterial("Oats", "Oats", 2, 12, 100, "kg");
            iib.createMaterial("Wheat Germ", "Wheat Germ", 2, 12, 20, "kg");
            iib.createMaterial("Cinnamon", "Cinnamon", 2, 12, 20, "kg");
            iib.createMaterial("Nutmeg", "Nutmeg", 2, 12, 20, "kg");
            iib.createMaterial("Cloves", "Cloves", 2, 12, 20, "kg");
            iib.createMaterial("Honey", "Honey", 2, 12, 20, "l");
            iib.createMaterial("Canola Oil", "Canola Oil", 2, 12, 20, "l");
            iib.createMaterial("Vitamin/Mineral Powder", "Vitamin/Mineral Powder", 2, 12, 10, "kg");
            iib.createMaterial("Carob Chips", "Carob Chips", 2, 12, 20, "kg");
            iib.createMaterial("Protein Powder", "Protein Powder", 2, 12, 10, "kg");
            iib.createMaterial("Cocoa Powder", "Cocoa Powder", 1, 24, 20, "kg");
            iib.createMaterial("Hazelnuts", "Hazelnuts", 2, 24, 50, "kg");
            iib.createMaterial("Cashew Nuts", "Cashew Nuts", 2, 24, 50, "kg");
            iib.createMaterial("Walnuts", "Walnuts", 2, 24, 50, "kg");
            iib.createMaterial("Almonds", "Almonds", 2, 24, 50, "kg");
            iib.createMaterial("Macadamias", "Macadamias", 2, 24, 50, "kg");
            iib.createMaterial("Redcurrants", "Redcurrants", 3, 6, 50, "kg");
            iib.createMaterial("Raisins", "Raisins", 1, 12, 50, "kg");
            iib.createMaterial("Dates", "Dates", 1, 12, 50, "kg");
            iib.createMaterial("Apricots", "Apricots", 3, 6, 50, "kg");
            iib.createMaterial("Kiwis", "Kiwis", 3, 6, 50, "kg");
            iib.createMaterial("Apples", "Apples", 1, 6, 50, "kg");
            iib.createMaterial("Peaches", "Peaches", 1, 6, 50, "kg");
            System.out.println("Initialization successful");
        } catch (Exception ex) {
            System.out.println("Initialization error");
            ex.printStackTrace();
        }
    }

    public void initializeProducts() {
        try {
            System.out.println("Initializing Products...");
            Long productId1 = iib.createProduct2("Classic Nutri Bar - Nuts", "Classic Nutri Bar - Nuts", 12, 12, 6, 50, 10, 7);
            Long productId2 = iib.createProduct2("Classic Nutri Bar - Fruits", "Classic Nutri Bar - Fruits", 12, 12, 7, 50, 11, 8);
            Long productId3 = iib.createProduct2("Premium Power Bar - Chocolate Nuts", "Premium Power Bar - Chocolate Nuts", 12, 13.32, 8, 45, 12, 9);
            Long productId4 = iib.createProduct2("Premium Power Bar - Redcurrant Fruits", "Premium Power Bar - Redcurrant Fruits", 12, 13.32, 9, 45, 14, 10);

            mmb.createBillOfMaterial(productId1, 200, "kg");
            mmb.createBillOfMaterial(productId2, 200, "kg");
            mmb.createBillOfMaterial(productId3, 200, "kg");
            mmb.createBillOfMaterial(productId4, 200, "kg");

            System.out.println("Product Initialization successful");

            System.out.println("Generate pricebook for each product.");
            double[][] arr1 = new double[5][4];
            arr1[0][0] = 0;
            arr1[0][1] = 100;
            arr1[0][2] = 0;
            arr1[0][3] = 0;

            arr1[1][0] = 101;
            arr1[1][1] = 200;
            arr1[1][2] = 5;
            arr1[1][3] = 10;

            arr1[2][0] = 201;
            arr1[2][1] = 300;
            arr1[2][2] = 10;
            arr1[2][3] = 15;

            arr1[3][0] = 301;
            arr1[3][1] = 400;
            arr1[3][2] = 15;
            arr1[3][3] = 20;

            arr1[4][0] = 401;
            arr1[4][1] = 10000;
            arr1[4][2] = 20;
            arr1[4][3] = 25;

            sop.createPriceBook("Pricebook 1", productId1, arr1);
            sop.createPriceBook("Pricebook 2", productId2, arr1);
            sop.createPriceBook("Pricebook 3", productId3, arr1);
            sop.createPriceBook("Pricebook 4", productId4, arr1);
            System.out.println("Pricebook initialization successful");


        } catch (Exception ex) {
            System.out.println("Initialization error");
            ex.printStackTrace();
        }
    }

    public void initializeBillOfMaterial1() {
        try {
            System.out.println("Initializing BillOfMaterials...");

            Long productId = iib.searchProductId("Classic Nutri Bar - Nuts");
            Long id1 = iib.searchMaterialId("Oats");
            mmb.createBOMEntry(productId, id1, 100, "kg");
            Long id2 = iib.searchMaterialId("Wheat Germ");
            mmb.createBOMEntry(productId, id2, 20, "kg");
            Long id3 = iib.searchMaterialId("Cinnamon");
            mmb.createBOMEntry(productId, id3, 2, "kg");
            Long id4 = iib.searchMaterialId("Nutmeg");
            mmb.createBOMEntry(productId, id4, 1, "kg");
            Long id5 = iib.searchMaterialId("Cloves");
            mmb.createBOMEntry(productId, id5, 0.5, "kg");
            Long id6 = iib.searchMaterialId("Honey");
            mmb.createBOMEntry(productId, id6, 5, "l");
            Long id7 = iib.searchMaterialId("Canola Oil");
            mmb.createBOMEntry(productId, id7, 3, "l");
            Long id8 = iib.searchMaterialId("Vitamin/Mineral Powder");
            mmb.createBOMEntry(productId, id8, 2, "kg");
            Long id9 = iib.searchMaterialId("Carob Chips");
            mmb.createBOMEntry(productId, id9, 20, "kg");
            Long id12 = iib.searchMaterialId("Hazelnuts");
            mmb.createBOMEntry(productId, id12, 15, "kg");
            Long id13 = iib.searchMaterialId("Cashew Nuts");
            mmb.createBOMEntry(productId, id13, 15, "kg");
            Long id14 = iib.searchMaterialId("Walnuts");
            mmb.createBOMEntry(productId, id14, 15, "kg");
            Long id15 = iib.searchMaterialId("Almonds");
            mmb.createBOMEntry(productId, id15, 10, "kg");
            Long id16 = iib.searchMaterialId("Macadamias");
            mmb.createBOMEntry(productId, id16, 10, "kg");

            System.out.println("BillOfMaterials Initialization successful");

        } catch (Exception ex) {
            System.out.println("Initialization error");
            ex.printStackTrace();
        }
    }

    public void initializeBillOfMaterial2() {
        try {
            System.out.println("Initializing BillOfMaterials...");

            Long productId = iib.searchProductId("Classic Nutri Bar - Fruits");

            Long id1 = iib.searchMaterialId("Oats");
            mmb.createBOMEntry(productId, id1, 100, "kg");
            Long id2 = iib.searchMaterialId("Wheat Germ");
            mmb.createBOMEntry(productId, id2, 20, "kg");
            Long id3 = iib.searchMaterialId("Cinnamon");
            mmb.createBOMEntry(productId, id3, 2, "kg");
            Long id4 = iib.searchMaterialId("Nutmeg");
            mmb.createBOMEntry(productId, id4, 1, "kg");
            Long id5 = iib.searchMaterialId("Cloves");
            mmb.createBOMEntry(productId, id5, 0.5, "kg");
            Long id6 = iib.searchMaterialId("Honey");
            mmb.createBOMEntry(productId, id6, 5, "l");
            Long id7 = iib.searchMaterialId("Canola Oil");
            mmb.createBOMEntry(productId, id7, 3, "l");
            Long id8 = iib.searchMaterialId("Vitamin/Mineral Powder");
            mmb.createBOMEntry(productId, id8, 2, "kg");
            Long id9 = iib.searchMaterialId("Carob Chips");
            mmb.createBOMEntry(productId, id9, 10, "kg");
            Long id12 = iib.searchMaterialId("Hazelnuts");
            mmb.createBOMEntry(productId, id12, 5, "kg");
            Long id17 = iib.searchMaterialId("Redcurrants");
            mmb.createBOMEntry(productId, id17, 5, "kg");
            Long id18 = iib.searchMaterialId("Raisins");
            mmb.createBOMEntry(productId, id18, 20, "kg");
            Long id19 = iib.searchMaterialId("Dates");
            mmb.createBOMEntry(productId, id19, 15, "kg");
            Long id20 = iib.searchMaterialId("Apricots");
            mmb.createBOMEntry(productId, id20, 10, "kg");
            Long id21 = iib.searchMaterialId("Kiwis");
            mmb.createBOMEntry(productId, id21, 10, "kg");
            Long id22 = iib.searchMaterialId("Apples");
            mmb.createBOMEntry(productId, id22, 10, "kg");
            Long id23 = iib.searchMaterialId("Peaches");
            mmb.createBOMEntry(productId, id23, 10, "kg");

            System.out.println("BillOfMaterials Initialization successful");

        } catch (Exception ex) {
            System.out.println("Initialization error");
            ex.printStackTrace();
        }
    }

    public void initializeBillOfMaterial3() {
        try {
            System.out.println("Initializing BillOfMaterials...");

            Long productId = iib.searchProductId("Premium Power Bar - Chocolate Nuts");

            Long id1 = iib.searchMaterialId("Oats");
            mmb.createBOMEntry(productId, id1, 120, "kg");
            Long id2 = iib.searchMaterialId("Wheat Germ");
            mmb.createBOMEntry(productId, id2, 25, "kg");
            Long id3 = iib.searchMaterialId("Cinnamon");
            mmb.createBOMEntry(productId, id3, 4, "kg");
            Long id4 = iib.searchMaterialId("Nutmeg");
            mmb.createBOMEntry(productId, id4, 1.5, "kg");
            Long id5 = iib.searchMaterialId("Cloves");
            mmb.createBOMEntry(productId, id5, 1, "kg");
            Long id6 = iib.searchMaterialId("Honey");
            mmb.createBOMEntry(productId, id6, 7, "l");
            Long id7 = iib.searchMaterialId("Canola Oil");
            mmb.createBOMEntry(productId, id7, 5, "l");
            Long id8 = iib.searchMaterialId("Vitamin/Mineral Powder");
            mmb.createBOMEntry(productId, id8, 5, "kg");
            Long id9 = iib.searchMaterialId("Carob Chips");
            mmb.createBOMEntry(productId, id9, 25, "kg");
            Long id10 = iib.searchMaterialId("Protein Powder");
            mmb.createBOMEntry(productId, id10, 25, "kg");
            Long id11 = iib.searchMaterialId("Cocoa Powder");
            mmb.createBOMEntry(productId, id11, 30, "kg");
            Long id12 = iib.searchMaterialId("Hazelnuts");
            mmb.createBOMEntry(productId, id12, 20, "kg");
            Long id13 = iib.searchMaterialId("Cashew Nuts");
            mmb.createBOMEntry(productId, id13, 20, "kg");
            Long id14 = iib.searchMaterialId("Walnuts");
            mmb.createBOMEntry(productId, id14, 20, "kg");
            Long id15 = iib.searchMaterialId("Almonds");
            mmb.createBOMEntry(productId, id15, 15, "kg");
            Long id16 = iib.searchMaterialId("Macadamias");
            mmb.createBOMEntry(productId, id16, 15, "kg");


            System.out.println("BillOfMaterials Initialization successful");

        } catch (Exception ex) {
            System.out.println("Initialization error");
            ex.printStackTrace();
        }
    }

    public void initializeBillOfMaterial4() {
        try {
            System.out.println("Initializing BillOfMaterials...");

            Long productId = iib.searchProductId("Premium Power Bar - Redcurrant Fruits");

            Long id1 = iib.searchMaterialId("Oats");
            mmb.createBOMEntry(productId, id1, 120, "kg");
            Long id2 = iib.searchMaterialId("Wheat Germ");
            mmb.createBOMEntry(productId, id2, 25, "kg");
            Long id3 = iib.searchMaterialId("Cinnamon");
            mmb.createBOMEntry(productId, id3, 4, "kg");
            Long id4 = iib.searchMaterialId("Nutmeg");
            mmb.createBOMEntry(productId, id4, 1.5, "kg");
            Long id5 = iib.searchMaterialId("Cloves");
            mmb.createBOMEntry(productId, id5, 1, "kg");
            Long id6 = iib.searchMaterialId("Honey");
            mmb.createBOMEntry(productId, id6, 7, "l");
            Long id7 = iib.searchMaterialId("Canola Oil");
            mmb.createBOMEntry(productId, id7, 5, "l");
            Long id8 = iib.searchMaterialId("Vitamin/Mineral Powder");
            mmb.createBOMEntry(productId, id8, 5, "kg");
            Long id9 = iib.searchMaterialId("Carob Chips");
            mmb.createBOMEntry(productId, id9, 15, "kg");
            Long id10 = iib.searchMaterialId("Protein Powder");
            mmb.createBOMEntry(productId, id10, 25, "kg");
            Long id11 = iib.searchMaterialId("Cocoa Powder");
            mmb.createBOMEntry(productId, id11, 5, "kg");
            Long id12 = iib.searchMaterialId("Hazelnuts");
            mmb.createBOMEntry(productId, id12, 7, "kg");
            Long id17 = iib.searchMaterialId("Redcurrants");
            mmb.createBOMEntry(productId, id17, 30, "kg");
            Long id18 = iib.searchMaterialId("Raisins");
            mmb.createBOMEntry(productId, id18, 15, "kg");
            Long id19 = iib.searchMaterialId("Dates");
            mmb.createBOMEntry(productId, id19, 15, "kg");
            Long id20 = iib.searchMaterialId("Apricots");
            mmb.createBOMEntry(productId, id20, 10, "kg");
            Long id21 = iib.searchMaterialId("Kiwis");
            mmb.createBOMEntry(productId, id21, 15, "kg");
            Long id22 = iib.searchMaterialId("Apples");
            mmb.createBOMEntry(productId, id22, 10, "kg");
            Long id23 = iib.searchMaterialId("Peaches");
            mmb.createBOMEntry(productId, id23, 15, "kg");


            System.out.println("BillOfMaterials Initialization successful");

        } catch (Exception ex) {
            System.out.println("Initialization error");
            ex.printStackTrace();
        }
    }

    public void addMaterial() {

        try {

            System.out.print("Name              : ");
            String name = scanner.nextLine();
            System.out.print("Description           : ");
            String description = scanner.nextLine();
            System.out.print("Lead Time (in weeks)  : ");
            int leadTime = Integer.parseInt(scanner.nextLine());
            System.out.print("Shelf Life (in months): ");
            int shelfLife = Integer.parseInt(scanner.nextLine());
            System.out.print("Lot Size       : ");
            int lotSize = Integer.parseInt(scanner.nextLine());
            System.out.print("Lot Size Unit  : ");
            String lotSizeUnit = scanner.nextLine();

            iib.createMaterial(name, description, leadTime, shelfLife, lotSize, lotSizeUnit);
            System.out.println("Material Created!");
        } catch (Exception ex) {
            System.out.println("create error");
            ex.printStackTrace();
        }
    }

    public void updateMaterial() {

        try {
            System.out.print("ID                    : ");
            Long id = Long.parseLong(scanner.nextLine());
            System.out.print("Name              : ");
            String name = scanner.nextLine();
            System.out.print("Description           : ");
            String description = scanner.nextLine();
            System.out.print("Lead Time (in weeks)  : ");
            int leadTime = Integer.parseInt(scanner.nextLine());
            System.out.print("Shelf Life (in months): ");
            int shelfLife = Integer.parseInt(scanner.nextLine());
            System.out.print("Lot Size       : ");
            int lotSize = Integer.parseInt(scanner.nextLine());
            System.out.print("Lot Size Unit  : ");
            String lotSizeUnit = scanner.nextLine();

            iib.updateMaterial(id, name, description, leadTime, lotSize, leadTime, shelfLife, lotSize, lotSizeUnit);
            System.out.println("Material Updated!");
        } catch (Exception ex) {
            System.out.println("update error");
            ex.printStackTrace();
        }
    }

    public void deleteMaterial() {

        try {
            System.out.print("Material ID to be deleted : ");
            Long id = Long.parseLong(scanner.nextLine());

            if (iib.deleteMaterial(id)) {
                System.out.println("Material Deleted!");
            } else {
                System.out.println("No such material");
            }
        } catch (Exception ex) {
            System.out.println("delete error");
            ex.printStackTrace();
        }
    }

    public void addProduct() {

        try {

            System.out.print("Name          : ");
            String name = scanner.nextLine();
            System.out.print("Description   : ");
            String description = scanner.nextLine();
            System.out.print("Shelf Life (in months)         : ");
            int shelfLife = Integer.parseInt(scanner.nextLine());
            System.out.print("Case Weight (in kg)            : ");
            int caseWeight = Integer.parseInt(scanner.nextLine());
            System.out.print("Base Price (in SGD)            : ");
            int basePrice = Integer.parseInt(scanner.nextLine());
            System.out.print("Production Rate (in case/hour) : ");
            int productionRate = Integer.parseInt(scanner.nextLine());

            iib.createProduct(name, description, shelfLife, caseWeight, basePrice, productionRate);
            System.out.println("Product Created!");
        } catch (Exception ex) {
            System.out.println("create error");
            ex.printStackTrace();
        }
    }

    public void updateProduct() {

        try {
            System.out.print("ID            :");
            Long id = Long.parseLong(scanner.nextLine());
            System.out.print("Name          : ");
            String name = scanner.nextLine();
            System.out.print("Description   : ");
            String description = scanner.nextLine();
            System.out.print("Shelf Life (in months)         : ");
            int shelfLife = Integer.parseInt(scanner.nextLine());
            System.out.print("Case Weight (in kg)            : ");
            int caseWeight = Integer.parseInt(scanner.nextLine());
            System.out.print("Base Price (in SGD)            : ");
            int basePrice = Integer.parseInt(scanner.nextLine());
            System.out.print("Production Rate (in case/hour) : ");
            int productionRate = Integer.parseInt(scanner.nextLine());

            double limitPrice = 0;
            double listPrice = 0;

            iib.updateProduct(id, name, description,
                    shelfLife, caseWeight, basePrice,
                    limitPrice, listPrice, productionRate);
            System.out.println("Product Updated!");
        } catch (Exception ex) {
            System.out.println("create error");
            ex.printStackTrace();
        }
    }

    public void deleteProduct() {

        try {
            System.out.print("ID            :");
            Long id = Long.parseLong(scanner.nextLine());

            if (iib.deleteProduct(id)) {
                System.out.println("Product Deleted");
            } else {
                System.out.println("No such product");
            }

        } catch (Exception ex) {
            System.out.println("create error");
            ex.printStackTrace();
        }
    }

    public void viewMaterials() {

        List<InventoryState> stateList = new ArrayList();
        stateList = iib.viewMaterials();
        System.out.println("\t\t\tMATERIAL LIST");
        System.out.println("***************************************************************");
        for (Object o : stateList) {
            InventoryState meState = (InventoryState) o;
            System.out.println("\t\tID             : " + meState.getItemId() + "\n"
                    + "\t\tNAME           : " + meState.getName() + "\n"
                    + "\t\tDESCRIPTION    : " + meState.getDescription() + "\n"
                    + "\t\tINVENTORY TYPE : " + meState.getType() + "\n"
                    + "\t\tIN HAND QTY    : " + meState.getInHandQty() + " case\n"
                    + "\t\tCOMMITTED QTY  : " + meState.getReservedQty() + " case\n"
                    + "\t\tLEAD TIME      : " + meState.getLeadTime() + " weeks\n"
                    + "\t\tSHELF LIFE     : " + meState.getShelfLife() + " months\n"
                    + "\t\tLOT SIZE       : " + meState.getLotSize() + "\n"
                    + "\t\tLOT SIZE UNIT  : " + meState.getLotSizeUnit());
            System.out.println("***************************************************************");
        }
    }

    public void viewProducts() {

        List<InventoryState> stateList = new ArrayList();
        stateList = iib.viewProducts();
        System.out.println("\t\t\tPRODUCT LIST");
        System.out.println("***************************************************************");
        for (Object o : stateList) {
            InventoryState peState = (InventoryState) o;
            System.out.println("\t\tID              : " + peState.getItemId() + "\n"
                    + "\t\tNAME            : " + peState.getName() + "\n"
                    + "\t\tDESCRIPTION     : " + peState.getDescription() + "\n"
                    + "\t\tINVENTORY TYPE  : " + peState.getType() + "\n"
                    + "\t\tIN HAND QTY     : " + peState.getInHandQty() + " case\n"
                    + "\t\tCOMMITTED QTY   : " + peState.getReservedQty() + " case\n"
                    + "\t\tSHELF LIFE      : " + peState.getShelfLife() + " months\n"
                    + "\t\tCASE WEIGHT     : " + peState.getCaseWeight() + " kg/case\n"
                    + "\t\tBASE PRICE      : SGD" + peState.getBasePrice() + "/bar\n"
                    + "\t\tPRODUCTION RATE : " + peState.getProductionRate() + " case/hour");
            System.out.println("***************************************************************");
        }
    }

    public void initializeMonthlySalesRecords() {
        try {
            List<InventoryState> stateList = new ArrayList();
            stateList = iib.viewProducts();

            Long id = stateList.get(0).getItemId();
            Long id2 = stateList.get(1).getItemId();
            Long id3 = stateList.get(2).getItemId();
            Long id4 = stateList.get(3).getItemId();
            System.out.println("Initializing Monthly Sales Records...");
            System.out.println("Product IDs: " + id + ", " + id2 + ", " + id3 + ", " + id4);
            dmb.createMonthlySalesRecord(id, 2010, 1, 2664, 600, 3264);
            dmb.createMonthlySalesRecord(id, 2010, 2, 2616, 400, 3016);
            dmb.createMonthlySalesRecord(id, 2010, 3, 3120, 200, 3320);
            dmb.createMonthlySalesRecord(id, 2010, 4, 1536, 400, 1936);
            dmb.createMonthlySalesRecord(id, 2010, 5, 1464, 800, 2264);
            dmb.createMonthlySalesRecord(id, 2010, 6, 2208, 600, 2808);
            dmb.createMonthlySalesRecord(id, 2010, 7, 1776, 800, 2576);
            dmb.createMonthlySalesRecord(id, 2010, 8, 2424, 0, 2424);
            dmb.createMonthlySalesRecord(id, 2010, 9, 1584, 800, 2384);
            dmb.createMonthlySalesRecord(id, 2010, 10, 2712, 200, 2912);
            dmb.createMonthlySalesRecord(id, 2010, 11, 2328, 400, 2728);
            dmb.createMonthlySalesRecord(id, 2010, 12, 1632, 800, 2432);
            dmb.createMonthlySalesRecord(id, 2011, 1, 1320, 0, 1320);
            dmb.createMonthlySalesRecord(id, 2011, 2, 1968, 0, 1968);
            dmb.createMonthlySalesRecord(id, 2011, 3, 3120, 0, 3120);
            dmb.createMonthlySalesRecord(id, 2011, 4, 2424, 400, 2824);
            dmb.createMonthlySalesRecord(id, 2011, 5, 2688, 600, 3288);
            dmb.createMonthlySalesRecord(id, 2011, 6, 2184, 0, 2184);
            dmb.createMonthlySalesRecord(id, 2011, 7, 2280, 600, 2880);
            dmb.createMonthlySalesRecord(id, 2011, 8, 3024, 600, 3624);
            dmb.createMonthlySalesRecord(id, 2011, 9, 3120, 600, 3720);
            dmb.createMonthlySalesRecord(id2, 2010, 1, 1536, 0, 1536);
            dmb.createMonthlySalesRecord(id2, 2010, 2, 2016, 200, 2216);
            dmb.createMonthlySalesRecord(id2, 2010, 3, 1440, 200, 1640);
            dmb.createMonthlySalesRecord(id2, 2010, 4, 2640, 200, 2840);
            dmb.createMonthlySalesRecord(id2, 2010, 5, 2400, 0, 2400);
            dmb.createMonthlySalesRecord(id2, 2010, 6, 1440, 200, 1640);
            dmb.createMonthlySalesRecord(id2, 2010, 7, 1320, 800, 2120);
            dmb.createMonthlySalesRecord(id2, 2010, 8, 2184, 200, 2384);
            dmb.createMonthlySalesRecord(id2, 2010, 9, 1488, 0, 1488);
            dmb.createMonthlySalesRecord(id2, 2010, 10, 1872, 200, 2072);
            dmb.createMonthlySalesRecord(id2, 2010, 11, 2520, 600, 3120);
            dmb.createMonthlySalesRecord(id2, 2010, 12, 3096, 200, 3296);
            dmb.createMonthlySalesRecord(id2, 2011, 1, 2352, 200, 2552);
            dmb.createMonthlySalesRecord(id2, 2011, 2, 1944, 400, 2344);
            dmb.createMonthlySalesRecord(id2, 2011, 3, 2328, 200, 2528);
            dmb.createMonthlySalesRecord(id2, 2011, 4, 2664, 400, 3064);
            dmb.createMonthlySalesRecord(id2, 2011, 5, 2496, 200, 2696);
            dmb.createMonthlySalesRecord(id2, 2011, 6, 1872, 0, 1872);
            dmb.createMonthlySalesRecord(id2, 2011, 7, 2880, 200, 3080);
            dmb.createMonthlySalesRecord(id2, 2011, 8, 1368, 0, 1368);
            dmb.createMonthlySalesRecord(id2, 2011, 9, 1392, 600, 1992);
            dmb.createMonthlySalesRecord(id3, 2010, 1, 2112, 600, 2712);
            dmb.createMonthlySalesRecord(id3, 2010, 2, 2976, 800, 3776);
            dmb.createMonthlySalesRecord(id3, 2010, 3, 1776, 400, 2176);
            dmb.createMonthlySalesRecord(id3, 2010, 4, 1320, 0, 1320);
            dmb.createMonthlySalesRecord(id3, 2010, 5, 1224, 400, 1624);
            dmb.createMonthlySalesRecord(id3, 2010, 6, 1968, 600, 2568);
            dmb.createMonthlySalesRecord(id3, 2010, 7, 2784, 0, 2784);
            dmb.createMonthlySalesRecord(id3, 2010, 8, 1272, 0, 1272);
            dmb.createMonthlySalesRecord(id3, 2010, 9, 2400, 600, 3000);
            dmb.createMonthlySalesRecord(id3, 2010, 10, 2160, 600, 2760);
            dmb.createMonthlySalesRecord(id3, 2010, 11, 2136, 0, 2136);
            dmb.createMonthlySalesRecord(id3, 2010, 12, 1872, 800, 2672);
            dmb.createMonthlySalesRecord(id3, 2011, 1, 1560, 200, 1760);
            dmb.createMonthlySalesRecord(id3, 2011, 2, 1728, 600, 2328);
            dmb.createMonthlySalesRecord(id3, 2011, 3, 3072, 600, 3672);
            dmb.createMonthlySalesRecord(id3, 2011, 4, 2664, 0, 2664);
            dmb.createMonthlySalesRecord(id3, 2011, 5, 2352, 400, 2752);
            dmb.createMonthlySalesRecord(id3, 2011, 6, 2952, 600, 3552);
            dmb.createMonthlySalesRecord(id3, 2011, 7, 3072, 400, 3472);
            dmb.createMonthlySalesRecord(id3, 2011, 8, 1272, 400, 1672);
            dmb.createMonthlySalesRecord(id3, 2011, 9, 2904, 0, 2904);
            dmb.createMonthlySalesRecord(id4, 2010, 1, 2760, 400, 3160);
            dmb.createMonthlySalesRecord(id4, 2010, 2, 1824, 200, 2024);
            dmb.createMonthlySalesRecord(id4, 2010, 3, 1200, 600, 1800);
            dmb.createMonthlySalesRecord(id4, 2010, 4, 1560, 400, 1960);
            dmb.createMonthlySalesRecord(id4, 2010, 5, 1416, 0, 1416);
            dmb.createMonthlySalesRecord(id4, 2010, 6, 2544, 800, 3344);
            dmb.createMonthlySalesRecord(id4, 2010, 7, 1464, 0, 1464);
            dmb.createMonthlySalesRecord(id4, 2010, 8, 2400, 200, 2600);
            dmb.createMonthlySalesRecord(id4, 2010, 9, 2976, 400, 3376);
            dmb.createMonthlySalesRecord(id4, 2010, 10, 1464, 400, 1864);
            dmb.createMonthlySalesRecord(id4, 2010, 11, 2904, 200, 3104);
            dmb.createMonthlySalesRecord(id4, 2010, 12, 2184, 0, 2184);
            dmb.createMonthlySalesRecord(id4, 2011, 1, 1200, 800, 2000);
            dmb.createMonthlySalesRecord(id4, 2011, 2, 1608, 400, 2008);
            dmb.createMonthlySalesRecord(id4, 2011, 3, 2424, 800, 3224);
            dmb.createMonthlySalesRecord(id4, 2011, 4, 2856, 0, 2856);
            dmb.createMonthlySalesRecord(id4, 2011, 5, 1728, 600, 2328);
            dmb.createMonthlySalesRecord(id4, 2011, 6, 2400, 600, 3000);
            dmb.createMonthlySalesRecord(id4, 2011, 7, 2592, 600, 3192);
            dmb.createMonthlySalesRecord(id4, 2011, 8, 2472, 200, 2672);
            dmb.createMonthlySalesRecord(id4, 2011, 9, 3120, 0, 3120);
            System.out.println("Updating Consolidated Sales Record compositions");
            dmb.updateSalesCompositionRatios();
            System.out.println("Initialization successful");

        } catch (Exception ex) {
            System.out.println("Initialization error");
            ex.printStackTrace();
        }
    }

    public void InitializeSalesForecast() {
        try {
            System.out.println("Initializing Sales Forecast..");
            dmb.forecastSales(2011, 1, 2012, 12, 20);
            for (int i = 1; i < 12; i++) {
                dmb.finalizeSingleMonthlyForecast(2011, i);
            }
            System.out.println("Initialization successful");
        } catch (Exception e) {
            System.out.println("Initialization error");
            e.printStackTrace();
        }
    }

    public void createMonthlySalesRecord() {

        try {
            System.out.print("Product ID : ");
            Long id = Long.parseLong(scanner.nextLine());
            System.out.print("Year       : ");
            Integer year = Integer.parseInt(scanner.nextLine());
            System.out.print("Month      : ");
            Integer month = Integer.parseInt(scanner.nextLine());
            System.out.print("Base Sales        : ");
            int base = Integer.parseInt(scanner.nextLine());
            System.out.print("Promotion Sales   : ");
            int promotion = Integer.parseInt(scanner.nextLine());
            System.out.print("Total Sales       : ");
            int sales = Integer.parseInt(scanner.nextLine());


            dmb.createMonthlySalesRecord(id, year, month, base, promotion, sales);
            System.out.println("Monthly Sales Created!");
        } catch (Exception ex) {
            System.out.println("create error");
            ex.printStackTrace();
        }
    }

    public void updateMonthlySalesRecord() {

        try {
            System.out.print("Product ID : ");
            Long id = Long.parseLong(scanner.nextLine());
            System.out.print("Year       : ");
            Integer year = Integer.parseInt(scanner.nextLine());
            System.out.print("Month      : ");
            Integer month = Integer.parseInt(scanner.nextLine());
            System.out.print("Base Sales        : ");
            int base = Integer.parseInt(scanner.nextLine());
            System.out.print("Promotion Sales   : ");
            int promotion = Integer.parseInt(scanner.nextLine());
            System.out.print("Total Sales       : ");
            int sales = Integer.parseInt(scanner.nextLine());
            boolean status = false;

            dmb.updateMonthlySalesRecord(id, year, month, base, promotion, sales, status);
            System.out.println("Monthly Sales Updated!");
        } catch (Exception ex) {
            System.out.println("Update error");
            ex.printStackTrace();
        }
    }

    public void deleteMonthlySalesRecord() {

        try {
            System.out.print("Product ID : ");
            Long id = Long.parseLong(scanner.nextLine());
            System.out.print("Year       : ");
            Integer year = Integer.parseInt(scanner.nextLine());
            System.out.print("Month      : ");
            Integer month = Integer.parseInt(scanner.nextLine());

            dmb.deleteMonthlySalesRecord(id, year, month);

            System.out.println("Monthly Sales deleted!");
        } catch (Exception ex) {
            System.out.println("Delete error");
            ex.printStackTrace();
        }
    }

    public void viewMonthlySalesRecords() {
        try {
            List<SalesRecordState> stateList = new ArrayList();
            stateList = dmb.viewMonthlySalesRecords(null);
            System.out.println("\t\t\tMONTHLY SALES RECORDS");
            System.out.println("***************************************************************");
            for (Object o : stateList) {
                SalesRecordState srState = (SalesRecordState) o;
                String pmdCreated;
                if (srState.isConverted()) {
                    pmdCreated = "YES";
                } else {
                    pmdCreated = "NO";
                }
                System.out.println("\t\tYEAR              : " + srState.getYearInt() + "\n"
                        + "\t\tMONTH               : " + this.getMonth(srState.getMonthInt()) + "\n"
                        + "\t\tPRODUCT ID          : " + srState.getProduct().getId() + "\n"
                        + "\t\tPROJECTED BASE      : " + srState.getProjectedBase() + " case\n"
                        + "\t\tPROJECTED PROMOTION : " + srState.getProjectedPromotion() + " case\n"
                        + "\t\tPROJECTED SALES     : " + srState.getProjectedSales() + " case\n"
                        + "\t\tACTUAL BASE         : " + srState.getActualBase() + " case\n"
                        + "\t\tACTUAL PROMOTION    : " + srState.getActualPromotion() + " case\n"
                        + "\t\tACTUAL SALES        : " + srState.getActualSales() + " case\n "
                        + "\t\tPLANNED DEMAND CREATED : " + pmdCreated);
                System.out.println("***************************************************************");
            }
        } catch (Exception e) {
            System.out.println("Aggregation error");
            e.printStackTrace();
        }
    }

    public void aggregateAnnualSales() {
        try {
            System.out.print("Year       : ");
            Integer year = Integer.parseInt(scanner.nextLine());
            SalesRecordState state = dmb.aggregateAnnualSales(year);
            System.out.println("\t\tANNUAL SALES AGGREGATE");
            System.out.println("***************************************************************");
            System.out.println("\t\tYEAR                      : " + state.getYearInt() + "\n"
                    + "\t\tTOTAL ACTUAL BASE         : " + state.getAnnualBase() + " case\n"
                    + "\t\tTOTAL ACTUAL PROMOTION    : " + state.getAnnualPromotion() + " case\n"
                    + "\t\tTOTAL ACTUAL SALES        : " + state.getAnnualSales() + " case\n ");
            for (Object[] o : state.getMsrs()) {
                System.out.println("*****+++++++++++*****");
                System.out.println("\t\t*****MONTH               : " + this.getMonth((Integer) o[0]) + "\n"
                        + "\t\t*****PRODUCT ID          : " + o[1] + "\n"
                        + "\t\t*****ACTUAL SALES        : " + o[2] + " case\n"
                        + "\t\t*****ACTUAL PROMOTION    : " + o[3] + " case\n"
                        + "\t\t*****ACTUAL BASE         : " + o[4] + " case\n ");
            }
            System.out.println("***************************************************************");

        } catch (Exception e) {
            System.out.println("Aggregation error");
            e.printStackTrace();
        }
    }

    private String getMonth(Integer monthInt) {
        String month;

        switch (monthInt) {
            case 0:
                month = "JANUARY";
                break;
            case 1:
                month = "FEBRUARY";
                break;
            case 2:
                month = "MARCH";
                break;
            case 3:
                month = "APRIL";
                break;
            case 4:
                month = "MAY";
                break;
            case 5:
                month = "JUNE";
                break;
            case 6:
                month = "JULY";
                break;
            case 7:
                month = "AUGUST";
                break;
            case 8:
                month = "SEPTEMBER";
                break;
            case 9:
                month = "OCTOBER";
                break;
            case 10:
                month = "NOVEMBER";
                break;
            case 11:
                month = "DECEMBER";
                break;
            default:
                month = "INVALID: " + monthInt;
        }

        return month;

    }

    public void aggregateAnnualForecasts() {
        try {
            System.out.print("Year       : ");
            Integer year = Integer.parseInt(scanner.nextLine());
            SalesRecordState state = dmb.aggregateAnnualForecast(year);
            System.out.println("\t\tANNUAL SALES FORECAST AGGREGATE");
            System.out.println("***************************************************************");
            System.out.println("\t\tYEAR                         : " + state.getYearInt() + "\n"
                    + "\t\tTOTAL PROJECTED BASE         : " + state.getAnnualBase() + " case\n"
                    + "\t\tTOTAL PROJECTED PROMOTION    : " + state.getAnnualPromotion() + " case\n"
                    + "\t\tTOTAL PROJECTED SALES        : " + state.getAnnualSales() + " case\n ");
            for (Object[] o : state.getMsrs()) {
                System.out.println("*****+++++++++++*****");
                for (Object ob : o) {
                    System.out.println("ob: " + ob);
                }
                System.out.println("\t\t*****MONTH               : " + this.getMonth((Integer) o[0]) + "\n"
                        + "\t\t*****PRODUCT ID          : " + o[1] + "\n"
                        + "\t\t*****PROJECTED SALES     : " + o[2] + " case\n"
                        + "\t\t*****PROJECTED PROMOTION : " + o[3] + " case\n"
                        + "\t\t*****PROJECTED BASE      : " + o[4] + " case\n "
                        + "\t\t*****PLANNED DEMAND CREATED: " + o[5] + "\n");
            }
            System.out.println("***************************************************************");

        } catch (Exception e) {
            System.out.println("Aggregation error");
            e.printStackTrace();
        }
    }

    public void createMonthlySalesForecast() {
        try {
            System.out.println("Create Monthly Forecast ");
            System.out.print("Product ID        : ");
            Long id = Long.parseLong(scanner.nextLine());
            System.out.print("Year              : ");
            Integer year = Integer.parseInt(scanner.nextLine());
            System.out.print("Month             : ");
            Integer month = Integer.parseInt(scanner.nextLine());
            System.out.print("Growth rate       : ");
            double growth = Double.parseDouble(scanner.nextLine());
            System.out.print("Promotion Sales   : ");
            int promotion = Integer.parseInt(scanner.nextLine());

            dmb.forecastMonthlySales(id, year, month, growth, promotion);

        } catch (Exception e) {
            System.out.println("Unexpected error");
            e.printStackTrace();
        }
    }

    public void initializeForecasts() {
        try {
            System.out.println("Initializing Forecasts...");
            List<InventoryState> stateList = new ArrayList();
            stateList = iib.viewProducts();

            Long id = stateList.get(0).getItemId();
            Long id2 = stateList.get(1).getItemId();
            Long id3 = stateList.get(2).getItemId();
            Long id4 = stateList.get(3).getItemId();

            dmb.forecastMonthlySales(id, 2011, 0, 10.7, 0);
            dmb.forecastMonthlySales(id, 2011, 1, 7.5, 0);
            dmb.forecastMonthlySales(id, 2011, 2, 7.1, 0);
            dmb.forecastMonthlySales(id, 2011, 3, 12.8, 400);
            dmb.forecastMonthlySales(id, 2011, 4, 9, 0);
            dmb.forecastMonthlySales(id, 2011, 5, 10.2, 0);
            dmb.forecastMonthlySales(id, 2011, 6, 8.2, 0);
            dmb.forecastMonthlySales(id, 2011, 7, 6.9, 0);
            dmb.forecastMonthlySales(id, 2011, 8, 13.7, 0);
            dmb.forecastMonthlySales(id, 2011, 9, 12.5, 800);
            dmb.forecastMonthlySales(id, 2011, 10, 6.7, 0);
            dmb.forecastMonthlySales(id, 2011, 11, 7.9, 0);
            dmb.forecastMonthlySales(id2, 2011, 0, 7.3, 0);
            dmb.forecastMonthlySales(id2, 2011, 1, 14.9, 200);
            dmb.forecastMonthlySales(id2, 2011, 2, 12.4, 0);
            dmb.forecastMonthlySales(id2, 2011, 3, 6.3, 800);
            dmb.forecastMonthlySales(id2, 2011, 4, 13.5, 0);
            dmb.forecastMonthlySales(id2, 2011, 5, 14.2, 0);
            dmb.forecastMonthlySales(id2, 2011, 6, 14.3, 800);
            dmb.forecastMonthlySales(id2, 2011, 7, 11.5, 0);
            dmb.forecastMonthlySales(id2, 2011, 8, 7.8, 200);
            dmb.forecastMonthlySales(id2, 2011, 9, 8.5, 0);
            dmb.forecastMonthlySales(id2, 2011, 10, 5.3, 200);
            dmb.forecastMonthlySales(id2, 2011, 11, 8.6, 600);
            dmb.forecastMonthlySales(id3, 2011, 0, 12.1, 0);
            dmb.forecastMonthlySales(id3, 2011, 1, 11.3, 0);
            dmb.forecastMonthlySales(id3, 2011, 2, 5.4, 0);
            dmb.forecastMonthlySales(id3, 2011, 3, 12.9, 0);
            dmb.forecastMonthlySales(id3, 2011, 4, 14.9, 800);
            dmb.forecastMonthlySales(id3, 2011, 5, 7.3, 0);
            dmb.forecastMonthlySales(id3, 2011, 6, 6.9, 800);
            dmb.forecastMonthlySales(id3, 2011, 7, 13.6, 0);
            dmb.forecastMonthlySales(id3, 2011, 8, 8, 0);
            dmb.forecastMonthlySales(id3, 2011, 9, 13.3, 0);
            dmb.forecastMonthlySales(id3, 2011, 10, 12.1, 0);
            dmb.forecastMonthlySales(id3, 2011, 11, 7.3, 0);
            dmb.forecastMonthlySales(id4, 2011, 0, 8.1, 0);
            dmb.forecastMonthlySales(id4, 2011, 1, 6.5, 0);
            dmb.forecastMonthlySales(id4, 2011, 2, 9.1, 0);
            dmb.forecastMonthlySales(id4, 2011, 3, 8.1, 0);
            dmb.forecastMonthlySales(id4, 2011, 4, 12.4, 0);
            dmb.forecastMonthlySales(id4, 2011, 5, 10.3, 0);
            dmb.forecastMonthlySales(id4, 2011, 6, 10.2, 0);
            dmb.forecastMonthlySales(id4, 2011, 7, 12.3, 200);
            dmb.forecastMonthlySales(id4, 2011, 8, 7.4, 0);
            dmb.forecastMonthlySales(id4, 2011, 9, 15, 600);
            dmb.forecastMonthlySales(id4, 2011, 10, 6.3, 200);
            dmb.forecastMonthlySales(id4, 2011, 11, 8.1, 0);

            System.out.println("Initialization successful");

        } catch (Exception e) {
            System.out.println("Initialization error");
            e.printStackTrace();
        }
    }

    public void getWorkingDaysInWeeksInMonth() {
        try {

            System.out.print("Enter year (YYYY): ");
            Integer yearInt = Integer.parseInt(scanner.nextLine());
            System.out.print("Enter month (MM) : ");
            Integer monthInt = Integer.parseInt(scanner.nextLine());

            if (monthInt > 12 || monthInt < 1) {
                throw new IllegalArgumentException("Please insert value from 1-12 for month");
            } else {
                monthInt--;
            }

            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.YEAR, yearInt);
            cal.set(Calendar.MONTH, monthInt);
            cal.set(Calendar.DAY_OF_MONTH, 1);
            cal.setFirstDayOfWeek(Calendar.MONDAY);
            cal.setMinimalDaysInFirstWeek(1);

            System.out.println("Date set to " + cal.getTime().toString());
            int last = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
            int nweek = 1;

            int count[] = {0, 0, 0, 0, 0, 0};

            for (int i = 0; i < last; i++) {
                if (!(cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)) {
                    count[nweek - 1]++;
                }
                cal.add(Calendar.DAY_OF_MONTH, 1);
                if (cal.get(Calendar.WEEK_OF_MONTH) > nweek) {
                    nweek = cal.get(Calendar.WEEK_OF_MONTH);
                }
            }

            System.out.println("\t\tNo of Working Days \t\tWeek Of Month");
            for (int i = 0; i < count.length; i++) {
                System.out.println("\t\t" + count[i] + "\t\t" + (i + 1));
            }

        } catch (Exception e) {
            System.out.println("Unexpected error");
            e.printStackTrace();
        }
    }

    public void getWorkingDaysInMonth() {
        try {

            System.out.print("Enter year (YYYY): ");
            Integer yearInt = Integer.parseInt(scanner.nextLine());
            System.out.print("Enter month (MM) : ");
            Integer monthInt = Integer.parseInt(scanner.nextLine());

            if (monthInt > 12 || monthInt < 1) {
                throw new IllegalArgumentException("Please insert value from 1-12 for month");
            } else {
                monthInt--;
            }

            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.YEAR, yearInt);
            cal.set(Calendar.MONTH, monthInt);
            cal.set(Calendar.DAY_OF_MONTH, 1);
            System.out.println("Date set to " + cal.getTime().toString());
            int last = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
            int count = 0;

            for (int i = 0; i < last; i++) {
                if (!(cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)) {
                    count++;
                }
                cal.add(Calendar.DAY_OF_MONTH, 1);
                System.out.println("Date set to " + cal.getTime().toString());
            }
            System.out.println("\t\tNo of Working Days: " + count);

        } catch (Exception e) {
            System.out.println("Unexpected error");
            e.printStackTrace();
        }
    }

    public void generatePlannedWeeklyDemands() {
        try {
            System.out.print("Enter Product ID : ");
            Long id = Long.parseLong(scanner.nextLine());
            System.out.print("Enter year (YYYY): ");
            Integer yearInt = Integer.parseInt(scanner.nextLine());
            System.out.print("Enter month (MM) : ");
            Integer monthInt = Integer.parseInt(scanner.nextLine());
            System.out.println("Generating Planned Weekly Demands...");
            dmb.generateWeeklyPlannedDemands(id, yearInt, monthInt);
            System.out.println("Generation successful");

        } catch (Exception e) {
            System.out.println("Unexpected error");
            e.printStackTrace();
        }

    }

    public void generatePlannedDailyDemands() {
        try {
            System.out.print("Enter Product ID : ");
            Long id = Long.parseLong(scanner.nextLine());
            System.out.print("Enter year (YYYY): ");
            Integer yearInt = Integer.parseInt(scanner.nextLine());
            System.out.print("Enter month (MM) : ");
            Integer monthInt = Integer.parseInt(scanner.nextLine());
            System.out.print("Enter week (WW)   : ");
            Integer weekInt = Integer.parseInt(scanner.nextLine());
            System.out.println("Generating Planned Daily Demands...");
            dmb.generateDailyPlannedDemands(id, yearInt, monthInt, weekInt);
            System.out.println("Generation successful");

        } catch (Exception e) {
            System.out.println("Unexpected error");
            e.printStackTrace();
        }

    }

    public void viewPlannedDemands() {
        try {
            System.out.println("Available View Option");
            System.out.println("1 - Planned Monthly Demands");
            System.out.println("2 - Planned Weekly Demands");
            System.out.println("3 - Planned Daily Demands");
            System.out.print("Enter View Option : ");
            int option = Integer.parseInt(scanner.nextLine());

            List<PlannedDemandState> stateList = new ArrayList();
            stateList = dmb.viewPlannedDemands(option);

            switch (option) {
                case 1:
                    System.out.println("\t\t\tPLANNED MONTHLY DEMAND LIST");
                    System.out.println("***************************************************************");
                    for (Object o : stateList) {
                        PlannedDemandState pdState = (PlannedDemandState) o;
                        System.out.println("\t\tID             : " + pdState.getId() + "\n"
                                + "\t\tYEAR           : " + pdState.getYearInt() + "\n"
                                + "\t\tMONTH          : " + this.getMonth(pdState.getMonthInt()) + "\n"
                                + "\t\tDEMAND QTY     : " + pdState.getMonthlyDemand() + " case(s)\n"
                                + "\t\tPRODUCT ID     : " + pdState.getProductId() + " \n");
                        System.out.println("***************************************************************");
                    }
                    break;
                case 2:
                    System.out.println("\t\t\tPLANNED WEEKLY DEMAND LIST");
                    System.out.println("***************************************************************");
                    for (Object o : stateList) {
                        PlannedDemandState pdState = (PlannedDemandState) o;
                        System.out.println("\t\tID             : " + pdState.getId() + "\n"
                                + "\t\tYEAR           : " + pdState.getYearInt() + "\n"
                                + "\t\tMONTH          : " + this.getMonth(pdState.getMonthInt()) + "\n"
                                + "\t\tWEEK           : " + pdState.getWeekOfMonth() + "\n"
                                + "\t\tDEMAND QTY     : " + pdState.getWeeklyDemand() + " case(s)\n"
                                + "\t\tPRODUCT ID     : " + pdState.getProductId() + " \n");
                        System.out.println("***************************************************************");
                    }
                    break;
                case 3:
                    System.out.println("\t\t\tPLANNED DAILY DEMAND LIST");
                    System.out.println("***************************************************************");
                    for (Object o : stateList) {
                        PlannedDemandState pdState = (PlannedDemandState) o;
                        System.out.println("\t\tID             : " + pdState.getId() + "\n"
                                + "\t\tYEAR           : " + pdState.getYearInt() + "\n"
                                + "\t\tMONTH          : " + this.getMonth(pdState.getMonthInt()) + "\n"
                                + "\t\tWEEK           : " + pdState.getWeekOfMonth() + "\n"
                                + "\t\tDAY            : " + pdState.getDayOfMonth() + "\n"
                                + "\t\tDEMAND QTY     : " + pdState.getDailyDemand() + " case(s)\n"
                                + "\t\tPRODUCT ID     : " + pdState.getProductId() + "\n");
                        System.out.println("***************************************************************");
                    }
                    break;
            }


        } catch (Exception e) {
            System.out.println("Unexpected error");
            e.printStackTrace();
        }
    }

    public void convertSalesForecastToPlannedDemand() {
        try {
            System.out.print("Enter Product ID : ");
            Long id = Long.parseLong(scanner.nextLine());
            System.out.print("Enter year (YYYY): ");
            Integer yearInt = Integer.parseInt(scanner.nextLine());
            System.out.print("Enter month (MM) : ");
            Integer monthInt = Integer.parseInt(scanner.nextLine());

            dmb.convertMonthlyProjectedSalesToPlannedDemand(id, yearInt, monthInt);

            System.out.println("Monthly Planned Demand created");
        } catch (Exception e) {
            System.out.println("Unexpected error");
            e.printStackTrace();
        }
    }

    public void createMonthlyPlannedDemand() {
        try {
            System.out.print("Enter Product ID : ");
            Long id = Long.parseLong(scanner.nextLine());
            System.out.print("Enter year (YYYY): ");
            Integer yearInt = Integer.parseInt(scanner.nextLine());
            System.out.print("Enter month (MM) : ");
            Integer monthInt = Integer.parseInt(scanner.nextLine());
            System.out.print("Enter demand     : ");
            int demand = Integer.parseInt(scanner.nextLine());

            dmb.createMonthlyPlannedDemand(id, yearInt, monthInt, demand);

            System.out.println("MonthlyPlannedDemand created");
        } catch (Exception e) {
            System.out.println("Unexpected error");
            e.printStackTrace();
        }

    }

    public void updateMonthlyPlannedDemand() {
        try {
            System.out.print("Enter Product ID : ");
            Long id = Long.parseLong(scanner.nextLine());
            System.out.print("Enter year (YYYY): ");
            Integer yearInt = Integer.parseInt(scanner.nextLine());
            System.out.print("Enter month (MM) : ");
            Integer monthInt = Integer.parseInt(scanner.nextLine());
            System.out.print("Enter demand     : ");
            int demand = Integer.parseInt(scanner.nextLine());

            dmb.updateMonthlyPlannedDemand(id, yearInt, monthInt, demand);

            System.out.println("MonthlyPlannedDemand updated");
        } catch (Exception e) {
            System.out.println("Unexpected error");
            e.printStackTrace();
        }

    }

    public void deleteMonthlyPlannedDemand() {
        try {
            System.out.print("Enter Product ID : ");
            Long id = Long.parseLong(scanner.nextLine());
            System.out.print("Enter year (YYYY): ");
            Integer yearInt = Integer.parseInt(scanner.nextLine());
            System.out.print("Enter month (MM) : ");
            Integer monthInt = Integer.parseInt(scanner.nextLine());

            dmb.deleteMonthlyPlannedDemand(id, yearInt, monthInt);

            System.out.println("MonthlyPlannedDemand deleted");
        } catch (Exception e) {
            System.out.println("Unexpected error");
            e.printStackTrace();
        }

    }

    public void updateWeeklyPlannedDemand() {
        try {
            System.out.print("Enter Product ID : ");
            Long id = Long.parseLong(scanner.nextLine());
            System.out.print("Enter year (YYYY): ");
            Integer yearInt = Integer.parseInt(scanner.nextLine());
            System.out.print("Enter month (MM) : ");
            Integer monthInt = Integer.parseInt(scanner.nextLine());
            System.out.print("Enter week (WW)  : ");
            Integer weekInt = Integer.parseInt(scanner.nextLine());
            System.out.print("Enter demand     : ");
            int demand = Integer.parseInt(scanner.nextLine());

            dmb.updateWeeklyPlannedDemand(id, yearInt, monthInt, weekInt, demand);

            System.out.println("WeeklyPlannedDemand updated");
        } catch (Exception e) {
            System.out.println("Unexpected error");
            e.printStackTrace();
        }

    }

    public void deleteWeeklyPlannedDemand() {
        try {
            System.out.print("Enter Product ID : ");
            Long id = Long.parseLong(scanner.nextLine());
            System.out.print("Enter year (YYYY): ");
            Integer yearInt = Integer.parseInt(scanner.nextLine());
            System.out.print("Enter month (MM) : ");
            Integer monthInt = Integer.parseInt(scanner.nextLine());
            System.out.print("Enter week (WW)  : ");
            Integer weekInt = Integer.parseInt(scanner.nextLine());

            dmb.deleteWeeklyPlannedDemand(id, yearInt, monthInt, weekInt);

            System.out.println("WeeklyPlannedDemand deleted");
        } catch (Exception e) {
            System.out.println("Unexpected error");
            e.printStackTrace();
        }

    }

    public void updateDailyPlannedDemand() {
        try {
            System.out.print("Enter Product ID : ");
            Long id = Long.parseLong(scanner.nextLine());
            System.out.print("Enter year (YYYY): ");
            Integer yearInt = Integer.parseInt(scanner.nextLine());
            System.out.print("Enter month (MM) : ");
            Integer monthInt = Integer.parseInt(scanner.nextLine());
            System.out.print("Enter week (WW)  : ");
            Integer weekInt = Integer.parseInt(scanner.nextLine());
            System.out.print("Enter date (DD)  : ");
            Integer dayInt = Integer.parseInt(scanner.nextLine());
            System.out.print("Enter demand     : ");
            int demand = Integer.parseInt(scanner.nextLine());

            dmb.updateDailyPlannedDemand(id, yearInt, monthInt, weekInt, dayInt, demand);

            System.out.println("DailyPlannedDemand updated");
        } catch (Exception e) {
            System.out.println("Unexpected error");
            e.printStackTrace();
        }

    }

    public void deleteDailyPlannedDemand() {
        try {
            System.out.print("Enter Product ID : ");
            Long id = Long.parseLong(scanner.nextLine());
            System.out.print("Enter year (YYYY): ");
            Integer yearInt = Integer.parseInt(scanner.nextLine());
            System.out.print("Enter month (MM) : ");
            Integer monthInt = Integer.parseInt(scanner.nextLine());
            System.out.print("Enter week (WW)  : ");
            Integer weekInt = Integer.parseInt(scanner.nextLine());
            System.out.print("Enter date (DD)  : ");
            Integer dayInt = Integer.parseInt(scanner.nextLine());

            dmb.deleteDailyPlannedDemand(id, yearInt, monthInt, weekInt, dayInt);

            System.out.println("DailyPlannedDemand deleted");
        } catch (Exception e) {
            System.out.println("Unexpected error");
            e.printStackTrace();
        }

    }

    public void initializePlannedDemands() {
        try {
            System.out.println("Generating Planned Monthly Demands from Sales Forecasts...");

            List<SalesRecordState> stateList = dmb.viewMonthlySalesRecords("WHERE m.yearInt = 2011");
            for (SalesRecordState sr : stateList) {
                dmb.convertMonthlyProjectedSalesToPlannedDemand(sr.getProduct().getId(), sr.getYearInt(), sr.getMonthInt());
            }

            System.out.println("Generation successful!");
        } catch (Exception e) {
            System.out.println("Unexpected error");
            e.printStackTrace();
        }
    }

    public void initializeMonthlyPlannedDemands() {
        try {
            System.out.println("Initializing Planned Monthly Demands (Sales & Operation Plan) from Sales Forecasts...");

            List<InventoryState> stateList = new ArrayList();
            stateList = iib.viewProducts();

            List<Object[]> productRatios = new ArrayList();
            int[] ratios = {30, 30, 20, 20};
            for (int i = 0; i < 4; i++) {
                Long id = stateList.get(i).getItemId();
                Object[] productRatio = new Object[2];
                productRatio[0] = id;
                productRatio[1] = ratios[i];
                productRatios.add(productRatio);
            }
            List<SalesOperationPlanState> states = new ArrayList();
            states.addAll(dmb.generateSalesOperationPlanByPeriod(14, 2011, productRatios));
            states.addAll(dmb.generateSalesOperationPlanByPeriod(11, 2012, productRatios));
            states.addAll(dmb.generateSalesOperationPlanByPeriod(12, 2012, productRatios));
            states.addAll(dmb.generateSalesOperationPlanByPeriod(13, 2012, productRatios));

            dmb.breakdownMonthPlannedDemands(2011, 10);
            dmb.breakdownMonthPlannedDemands(2011, 11);
            dmb.breakdownMonthPlannedDemands(2011, 12);
            dmb.breakdownMonthPlannedDemands(2012, 1);
            dmb.breakdownMonthPlannedDemands(2012, 2);
            dmb.breakdownMonthPlannedDemands(2012, 3);

            System.out.println("Initializing successful!");
        } catch (Exception e) {
            System.out.println("Unexpected error");
            e.printStackTrace();
        }
    }

    public void initializeMaterialRequirements() {
        try {
            System.out.println("Initializing material requirements..");
            List<PlannedDemandState> states = new ArrayList();
            states = dmb.viewPlannedDemands(2);

            for (PlannedDemandState state : states) {
                System.out.println("Generating weekly material requirement for week " + state.getWeekOfMonth() + " (" + state.getMonthInt() + "/" + state.getYearInt() + ")");
                String wmrId = mmb.createWeeklyMaterialRequirementByProduct(state.getId(), state.getExecDate());
                System.out.println("Synchronizing indiviual material requirements for the week");
                mmb.generateIndividualWeeklyMaterialRequirement(wmrId);
            }

            System.out.println("Initialization successful.. ");
        } catch (Exception e) {
            System.out.println("Material Requirements initialization failed");
            e.printStackTrace();
        }
    }

    public void initializeInventoryBatches() {
        try {
            System.out.println("Initializing material batches for the last 2 months");
            List<InventoryState> materials = iib.viewMaterials();

            for (InventoryState material : materials) {
                System.out.println("Initializing requirement plannings");
                mmb.viewMRPRecord(material.getItemId(), 2011, 9);
                mmb.viewMRPRecord(material.getItemId(), 2011, 10);
                mmb.viewMRPRecord(material.getItemId(), 2011, 11);
            }

            System.out.println("Purchasing the required materials");
            pmb.initializeMaterialBatches();
            System.out.println("Creating the product batches");
            pmb.initializeProductBatches();
            System.out.println("initialization successful");
        } catch (Exception e) {
            System.out.println("Initialization failed");
            e.printStackTrace();
        }
    }

    public void testViewMRP() {
        try {
            System.out.println("Testing MRP View...");
            System.out.print("Enter Material ID : ");
            Long id = Long.parseLong(scanner.nextLine());
            System.out.print("Enter year (YYYY): ");
            Integer yearInt = Integer.parseInt(scanner.nextLine());
            System.out.print("Enter month (MM) : ");
            Integer monthInt = Integer.parseInt(scanner.nextLine());
            MRPRecordState mrpState = mmb.viewMRPRecord(id, yearInt, monthInt);
            System.out.println("------------------------------------------------------------");
            System.out.println("MRP of " + mrpState.getMaterialName() + " for " + mrpState.getMonthInt() + "/" + mrpState.getYearInt());
            System.out.println("*** Leadtime = " + mrpState.getLeadtime());
            System.out.println("*** Beginning Inventory = " + mrpState.getBeginningInventory());
            for (IndividualWeeklyRequirementState imrState : mrpState.getWeeklyMaterialStates()) {
                System.out.println("***++*** Week " + imrState.getWeekInt() + "***++***");
                for (WeeklyRequirementBreakdownState breakdown : imrState.getBreakdowns()) {
                    System.out.println("----" + breakdown.getProductName() + ": " + breakdown.getRequiredCase());
                }
                System.out.println("\t\t\t ---------------------------------");
                System.out.println("\t\t Total MPS (Case): " + imrState.getRequiredTotalCase());
                for (WeeklyRequirementBreakdownState breakdown : imrState.getBreakdowns()) {
                    System.out.println("----" + breakdown.getProductName() + ": " + breakdown.getRequiredKgBatch());
                }
                System.out.println("\t\t\t ---------------------------------");
                System.out.println("\t\t Total MPS (unit batch) : " + imrState.getRequiredTotalKgBatch());
                System.out.println("\t\t Gross Requirement      : " + imrState.getGrossRequirement());
                System.out.println("\t\t Planned Receipt        : " + imrState.getPlannedReceipt());
                System.out.println("\t\t On Hand                : " + imrState.getInHandInventory());
                System.out.println("\t\t Planned Order          : " + imrState.getPlannedOrder());
            }
            System.out.println("------------------------------------------------------------");


        } catch (Exception e) {
            System.out.println("Test failed..");
            e.printStackTrace();
        }
    }

    public void generateWeeklyPlannedDemands() {
        try {
            System.out.println("Generating Planned Weekly Demands...");

            List<PlannedDemandState> stateList = new ArrayList();
            stateList = dmb.viewPlannedDemands(1);

            for (PlannedDemandState pd : stateList) {
                dmb.generateWeeklyPlannedDemands(pd.getProductId(), pd.getYearInt(), pd.getMonthInt());
            }

            System.out.println("Generation successful!");
        } catch (Exception e) {
            System.out.println("Unexpected error");
            e.printStackTrace();
        }
    }

    public void generateDailyPlannedDemands() {
        try {
            List<PlannedDemandState> stateList = new ArrayList();
            stateList = dmb.viewPlannedDemands(2);
            System.out.println("Generating Planned Daily Demands...");

            for (PlannedDemandState pd : stateList) {
                dmb.generateDailyPlannedDemands(pd.getProductId(), pd.getYearInt(), pd.getMonthInt(), pd.getWeekOfMonth());
            }
            System.out.println("Generation successful!");
        } catch (Exception e) {
            System.out.println("Unexpected error");
            e.printStackTrace();
        }
    }

    public void initializeSalesLead() {

        DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
        Date dateIssued = new Date();
        String date = "20/08/1989";
        try {
            dateIssued = df.parse(date);
        } catch (ParseException ex) {
            ex.printStackTrace();
        }


        smb.addSalesLead("Mrs.", "Jane", "William", "Barclays Pte Ltd", "3457435OK", "Banking", 80000, 15000, "9856736", "8576483", "98675644",
                "www.barclays.com", 5, "19th Raffles Square", "GA", "USA", "New York", "1504", "janewilliam@barclays.com",
                "Wholesale", "Advertisement", "Sales Manager", "Sales Department", dateIssued, "No description found.");

        smb.addSalesLead("Mr.", "Robert", "Humphrey", "Creative Pte Ltd", "0123123JTU", "Entertainment", 50000, 8000, "8456456", "745646", "9567567",
                "www.creative.com", 4, "202th Madison Square", "VA", "USA", "New York", "1766", "roberthumphrey@creative.com",
                "Wholesale", "External Referral", "Sales Manager", "Sales Department", dateIssued, "No description found.");

        smb.addSalesLead("Mr.", "Michael", "Christian", "Brilliant Pte Ltd", "224144KJJ", "Technology", 8000, 2000, "958345654", "857456", "89456456",
                "www.brilliant.com", 3, "177th Park Avenue", "SA", "USA", "San Fransisco", "545", "michris@brilliant.com",
                "Direct", "Trade Show", "Sales Manager", "Sales Department", dateIssued, "No description found.");

        smb.addSalesLead("Ms.", "Keira", "Hunt", "Oracle Pte Ltd", "349234DFO", "Consulting", 65000, 10000, "9834953", "8854353", "9274234",
                "www.oracle.com", 4, "188th Central Park Avenue", "NY", "USA", "New York", "7456", "keirahunt@oracle.com",
                "Wholesale", "Employees Referral", "Sales Manager", "Sales Department", dateIssued, "No description found.");

        smb.addSalesLead("Dr.", "Kevin", "Jackson", "Alexandra Hospital", "0932840BR", "Healthcare", 88000, 12000, "8342384", "8324324", "9182313",
                "www.alexandrahospital.com", 4, "29th Graham Central", "GA", "USA", "New York", "1502", "kevinjackson@alexandra.com",
                "Wholesale", "Partner", "Sales Manager", "Sales Department", dateIssued, "No description found.");
    }

    public void initializeCustomers() {
        System.out.println("Initialize Customers");
        DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
        Date dateIssued = new Date();
        String date = "20/08/1986";
        try {
            dateIssued = df.parse(date);
        } catch (ParseException ex) {
            ex.printStackTrace();
        }

        smb.addCustomer2("Seven Eleven", "B94585MB", "Retail", 12000, 1200, "8459359", "834234", "www.7eleven.com", 4, "18th Square Street", "Ohio", "USA", "California", "213", "seveneleven@gmail.com", "Wholesale", "No description found.", "anindya.pradipta1004@gmail.com", "Public", "AAA", "001");
        smb.addCustomer2("Acer Pte Ltd", "3423434B", "Technology", 60000, 6000, "84834849", "8932489", "www.acer.com", 4, "198th Madison Square", "Austin", "USA", "Virginia", "314", "acer@gmail.com", "Wholesale", "No description found.", "irvan.sudji@gmail.com", "Public", "AAB", "002");
        smb.addCustomer2("Adidas ", "34230203F", "Retail", 50000, 4000, "892304", "8402442", "www.adidas.com", 3, "3rd East Street", "Oklahoma", "USA", "New York", "265", "adidas@yahoo.com", "Direct", "No description found.", "irvan.sudji@gmail.com", "Public", "ABA", "003");
        smb.addCustomer2("Dell Pte Ltd", "24987324CV", "Technology", 45000, 3000, "7429394", "239483294", "www.dell.com", 4, "11th West Street", "San Fransisco", "USA", "San Fransisco", "348", "dell@gmail.com", "Wholesale", "No description found.", "anindya.pradipta1004@gmail.com", "Public", "BBB", "004");
        smb.addCustomer2("Procter and Gamble", "34234234NB", "Chemicals", 88000, 10000, "39549548", "984593548", "www.proctergamble.com", 3, "15th Northway", "New Mexico", "USA", "Texas", "954", "proctergamble@gmail.com", "Wholesale", "No description found.", "anindya.pradipta1004@gmail.com", "Public", "ABA", "005");
        smb.addCustomer2("McDonalds' Holding", "9438474MK", "Food and Beverage", 123000, 12000, "923494", "843948", "www.mcdholding.com", 4, "199th Southeast Road", "Northwest", "USA", "Washington", "455", "mcdonalds@gmail.com", "Direct", "No description found.", "irvan.sudji@gmail.com", "Public", "ABB", "006");
        smb.addCustomer2("Pizza Hut", "123390BC", "Food and Beverage", 90000, 10000, "289237", "38789409", "www.pizzahut.com.my", 4, "154,  Robinson Road", "Upper East", "USA", "Virginia", "899", "pizzahut@gmail.com", "DIrect", "No description found.", "irvan.sudji@gmail.com", "Public", "ABA", "007");
        smb.addCustomer2("Good Food Holding", "039434PX", "Food and Beverage", 10000, 5000, "76746543", "234598754", "www.goodfood.com.my", 2, "132, West Coast Road", "East Coast", "USA", "Chigago", "233", "goodfood@hotmail.com", "Wholesale", "No description found.", "irvan.sudji@gmail.com", "Public", "AAA", "008");
        smb.addCustomer2("Cheers", "834343NB", "Retail", 15000, 2000, "67876483", "2987483", "www.cheers.com.my", 3, "153, Nottinghill", "NottingHill", "USA", "San Fransisco", "677", "cheers@hotmail.com", "Direct", "No description found.", "anindya.pradipta1004@gmail.com", "Public", "BBB", "009");
        smb.addCustomer2("Fig N Olive", "1029383KG", "Food and Beverage", 18000, 2300, "3724893", "2546374", "www.figNolive.com.my", 3, "100, South Street", "Manhattan", "USA", "Texas", "888", "figNolive@gmail.com", "Wholesale", "No description found.", "anindya.pradipta1004@gmail.com", "Public", "ABB", "010");
        smb.addCustomer2("Fujitsu", "9340348HU", "Technology", 30000, 5000, "38479837", "938423", "www.fujitsu.com.us", 4, "15, Marry Road", "DC", "USA", "California", "988", "fujitsu@gmail.com", "Wholesale", "No description found.", "irvan.sudji@gmail.com", "Public", "AAB", "011");

        List<ClientState> customers = smb.viewCustomers2();
        Long cid1 = customers.get(0).getId();
        Long cid2 = customers.get(1).getId();
        Long cid3 = customers.get(2).getId();
        Long cid4 = customers.get(3).getId();
        Long cid5 = customers.get(4).getId();
        Long cid6 = customers.get(5).getId();
        Long cid7 = customers.get(6).getId();
        Long cid8 = customers.get(7).getId();
        Long cid9 = customers.get(8).getId();
        Long cid10 = customers.get(9).getId();
        Long cid11 = customers.get(10).getId();

        Long returnId[];

        returnId = smb.createBillingShippingAddress(cid1, "18th Street", "SF", "NY", "111", "USA", "18th Street", "SF", "NY", "111", "USA");
        smb.setPrimaryBillingAddress(returnId[0], cid1);
        smb.setPrimaryShippingAddress(returnId[1], cid1);

        returnId = smb.createBillingShippingAddress(cid2, "19th Street", "CA", "NY", "123", "USA", "134th Street", "CA", "NY", "1341", "USA");
        smb.setPrimaryBillingAddress(returnId[0], cid2);
        smb.setPrimaryShippingAddress(returnId[1], cid2);

        returnId = smb.createBillingShippingAddress(cid3, "112th Street", "TX", "UNC", "121", "USA", "38th Street", "LO", "CA", "342", "USA");
        smb.setPrimaryBillingAddress(returnId[0], cid3);
        smb.setPrimaryShippingAddress(returnId[1], cid3);

        returnId = smb.createBillingShippingAddress(cid4, "28th Street", "NC", "COL", "311", "USA", "156th Street", "TX", "COL", "434", "USA");
        smb.setPrimaryBillingAddress(returnId[0], cid4);
        smb.setPrimaryShippingAddress(returnId[1], cid4);

        returnId = smb.createBillingShippingAddress(cid5, "778th Street", "VI", "WA", "451", "USA", "119th Street", "KR", "NY", "488", "USA");
        smb.setPrimaryBillingAddress(returnId[0], cid5);
        smb.setPrimaryShippingAddress(returnId[1], cid5);

        returnId = smb.createBillingShippingAddress(cid6, "868th Street", "CN", "LA", "154", "USA", "28th Street", "SF", "LA", "289", "USA");
        smb.setPrimaryBillingAddress(returnId[0], cid6);
        smb.setPrimaryShippingAddress(returnId[1], cid6);

        returnId = smb.createBillingShippingAddress(cid7, "185th Street", "SF", "NY", "111", "USA", "198th Street", "SF", "NY", "111", "USA");
        smb.setPrimaryBillingAddress(returnId[0], cid7);
        smb.setPrimaryShippingAddress(returnId[1], cid7);

        returnId = smb.createBillingShippingAddress(cid8, "234th Street", "CA", "NY", "123", "USA", "135th Street", "CA", "NY", "1341", "USA");
        smb.setPrimaryBillingAddress(returnId[0], cid8);
        smb.setPrimaryShippingAddress(returnId[1], cid8);

        returnId = smb.createBillingShippingAddress(cid9, "132th Street", "TX", "UNC", "121", "USA", "39th Street", "LO", "CA", "342", "USA");
        smb.setPrimaryBillingAddress(returnId[0], cid9);
        smb.setPrimaryShippingAddress(returnId[1], cid9);

        returnId = smb.createBillingShippingAddress(cid10, "488th Street", "NC", "COL", "311", "USA", "6th Street", "TX", "COL", "434", "USA");
        smb.setPrimaryBillingAddress(returnId[0], cid10);
        smb.setPrimaryShippingAddress(returnId[1], cid10);

        returnId = smb.createBillingShippingAddress(cid11, "708th Street", "VI", "WA", "451", "USA", "109th Street", "KR", "NY", "488", "USA");
        smb.setPrimaryBillingAddress(returnId[0], cid11);
        smb.setPrimaryShippingAddress(returnId[1], cid11);

        System.out.println("Initialize Contacts");


        smb.addContact("Mrs.", "Jeane", "Robert", cid1, "Barclays Pte Ltd", "Advertisement", "Sales Manager", "Sales Department",
                "9856736", "8576483", "98675644", "jeanerobert@gmail.com",
                "29th Raffles Square", "GA", "USA", "New York", "13404",
                dateIssued, "No description found.");

        smb.addContact("Mrs.", "Tania", "Kern", cid2, "Barclays Pte Ltd", "Advertisement", "Sales Manager", "Sales Department",
                "9856736", "8576483", "98675644", "taniakern@hotmail.com",
                "139th Raffles Square", "GA", "USA", "New York", "12604",
                dateIssued, "No description found.");
        smb.addContact("Mrs.", "Cynthia", "Jennings", cid3, "Barclays Pte Ltd", "Advertisement", "Sales Manager", "Sales Department",
                "9856736", "8576483", "98675644", "cynthiajennings@live.com",
                "1339th Raffles Square", "SA", "USA", "New York", "1554",
                dateIssued, "No description found.");
        smb.addContact("Mr.", "Thomas", "Jefferson", cid4, "Barclays Pte Ltd", "Advertisement", "Sales Manager", "Sales Department",
                "9856736", "8576483", "98675644", "thomasjefferson@yahoo.com",
                "159th Raffles Square", "BA", "USA", "New York", "1244",
                dateIssued, "No description found.");
        smb.addContact("Mr.", "Jack", "Johnson", cid5, "Barclays Pte Ltd", "Advertisement", "Sales Manager", "Sales Department",
                "9856736", "8576483", "98675644", "jackjohnson@hotmail.com",
                "193th Raffles Square", "SF", "USA", "New York", "114",
                dateIssued, "No description found.");
        smb.addContact("Ms.", "Sophia", "William", cid6, "Barclays Pte Ltd", "Advertisement", "Sales Manager", "Sales Department",
                "9856736", "8576483", "98675644", "sophiawilliam@gmail.com",
                "189th Raffles Square", "YO", "USA", "New York", "22504",
                dateIssued, "No description found.");
        smb.addContact("Mr.", "Simon", "Peter", cid7, "Barclays Pte Ltd", "Advertisement", "Sales Manager", "Sales Department",
                "9856736", "8576483", "98675644", "simonpeter@yahoo.com",
                "199th Raffles Square", "YA", "USA", "New York", "231504",
                dateIssued, "No description found.");
        smb.addContact("Mrs.", "Leah", "Michelle", cid8, "Barclays Pte Ltd", "Advertisement", "Sales Manager", "Sales Department",
                "9856736", "8576483", "98675644", "leah@gmail.com",
                "19th Raffles Square", "LA", "USA", "New York", "141504",
                dateIssued, "No description found.");
        smb.addContact("Ms.", "Kurt", "Chriss", cid9, "Barclays Pte Ltd", "Advertisement", "Sales Manager", "Sales Department",
                "9856736", "8576483", "98675644", "kurt.c@hotmail.com",
                "129th Raffles Square", "PA", "USA", "New York", "131504",
                dateIssued, "No description found.");
        smb.addContact("Mr.", "Christopher", "Reed", cid10, "Barclays Pte Ltd", "Advertisement", "Sales Manager", "Sales Department",
                "9856736", "8576483", "98675644", "christopher@yahoo.com",
                "119th Raffles Square", "PE", "USA", "New York", "150443",
                dateIssued, "No description found.");
        smb.addContact("Mrs.", "Kimberly", "Chua", cid11, "Barclays Pte Ltd", "Advertisement", "Sales Manager", "Sales Department",
                "9856736", "8576483", "98675644", "kimchua@gmail.com",
                "319th Raffles Square", "GK", "USA", "New York", "1421",
                dateIssued, "No description found.");


    }

    public void initializeWarehouses() {
        try {
            System.out.println("Initializing warehouses..");
            iib.addWarehouse("Main HQ", 100000, "123 Somewhere St", "Paya Lebar", "Singapore", "575293");
            iib.addWarehouse("North Distribution Centre", 30000, "123 Somewhere North", "Sembawang", "Singapore", "654321");
            iib.addWarehouse("West Distribution Centre", 50000, "123 Somewhere West", "Jurong East", "Singapore", "248351");
            System.out.println("Warehouses initialized!");
        } catch (Exception e) {
            System.out.println("Initialization failed!");
            e.printStackTrace();
        }
    }

    public void searchProduct() {


        System.out.print("Enter id:");
        Long id = scanner.nextLong();
        ProductEntity product = iib.searchProduct(id);
        try {
            if (product != null) {

                System.out.println("Product Id :" + product.getId());
                System.out.println("Product name :" + product.getName());
                System.out.println("Product description :" + product.getDescription());
                System.out.println("Product shelfLife :" + product.getShelfLife());
                System.out.println("Product case weight : " + product.getCaseWeight());
                System.out.println("Product production rate :" + product.getProductionRate());
                System.out.println("Product BasePrice :" + product.getBasePrice());

            } else {
                System.out.println("Invalid product ID");
            }
        } catch (Exception ex) {
            System.out.println("Delete error");
            ex.printStackTrace();
        }
    }

    public void searchMaterial() {


        System.out.print("Enter id:");
        Long id = scanner.nextLong();
        MaterialEntity material = iib.searchMaterial(id);
        try {
            if (material != null) {

                System.out.println("Material Id :" + material.getId());
                System.out.println("Material name :" + material.getName());
                System.out.println("Material description :" + material.getDescription());
                System.out.println("Material shelfLife :" + material.getShelfLife());
                System.out.println("Material Lead Time : " + material.getLeadTime());
                System.out.println("Material Lot Size :" + material.getLotSize());
                System.out.println("Material Lot Size Unit :" + material.getLotSizeUnit());

            } else {
                System.out.println("Invalid material ID");
            }
        } catch (Exception ex) {
            System.out.println("Search error");
            ex.printStackTrace();
        }
    }

    public void createBillOfMaterial() {
        try {
            System.out.print("Enter Product ID : ");
            Long id = Long.parseLong(scanner.nextLine());
            System.out.print("Enter Batch Size      : ");
            int batchSize = Integer.parseInt(scanner.nextLine());
            System.out.print("Enter Batch Size Unit : ");
            String batchUnit = scanner.nextLine();

            mmb.createBillOfMaterial(id, batchSize, batchUnit);
            System.out.println("Bill of Material created");

        } catch (Exception e) {
            System.out.println("Create failed");
            e.printStackTrace();
        }

    }

    public void updateBillOfMaterial() {
        try {
            System.out.print("Enter Product ID : ");
            Long id = Long.parseLong(scanner.nextLine());
            System.out.print("Enter Batch Size      : ");
            int batchSize = Integer.parseInt(scanner.nextLine());
            System.out.print("Enter Batch Size Unit : ");
            String batchUnit = scanner.nextLine();

            mmb.updateBillOfMaterial(id, batchSize, batchUnit);
            System.out.println("Bill of Material updated");

        } catch (Exception e) {
            System.out.println("Update failed");
            e.printStackTrace();
        }

    }

    public void deleteBillOfMaterial() {
        try {
            System.out.print("Enter Product ID : ");
            Long id = Long.parseLong(scanner.nextLine());

            mmb.deleteBillOfMaterial(id);
            System.out.println("Bill of Material deleted");

        } catch (Exception e) {
            System.out.println("Update failed");
            e.printStackTrace();
        }
    }

    public void createBillOfMaterialEntry() {
        try {
            System.out.print("Enter Product ID    : ");
            Long productId = Long.parseLong(scanner.nextLine());
            System.out.print("Enter Material ID   : ");
            Long materialId = Long.parseLong(scanner.nextLine());
            System.out.print("Enter Material Qty  : ");
            int qty = Integer.parseInt(scanner.nextLine());
            System.out.print("Enter Quantity Unit : ");
            String unit = scanner.nextLine();


            mmb.createBOMEntry(productId, materialId, qty, unit);
            System.out.println("Bill of Material Entry created");

        } catch (Exception e) {
            System.out.println("Create failed");
            e.printStackTrace();
        }

    }

    public void updateBillOfMaterialEntry() {
        try {
            System.out.print("Enter Bill of Material ID    : ");
            Long entryId = Long.parseLong(scanner.nextLine());
            System.out.print("Enter Material ID   : ");
            Long materialId = Long.parseLong(scanner.nextLine());
            System.out.print("Enter Material Qty  : ");
            int qty = Integer.parseInt(scanner.nextLine());
            System.out.print("Enter Quantity Unit : ");
            String unit = scanner.nextLine();


            mmb.updateBOMEntry(entryId, materialId, qty, unit);
            System.out.println("Bill of Material Entry updated");

        } catch (Exception e) {
            System.out.println("Create failed");
            e.printStackTrace();
        }

    }

    public void deleteBillOfMaterialEntry() {
        try {
            System.out.print("Enter Bill of Material ID    : ");
            Long entryId = Long.parseLong(scanner.nextLine());

            mmb.deleteBOMEntry(entryId);

            System.out.println("Bill of Material Entry deleted");
        } catch (Exception e) {
            System.out.println("Create failed");
            e.printStackTrace();
        }

    }

    public void viewProductBillOfMaterial() {
        try {
            System.out.print("Enter Product ID    : ");
            Long id = Long.parseLong(scanner.nextLine());

            BillOfMaterialState state = mmb.viewProductBillOfMaterial(id);


            System.out.println("\t\t\tPRODUCT BILL OF MATERIAL");
            System.out.println("***************************************************************");
            System.out.println("\t\t\tPRODUCT ID : " + state.getProductId());
            System.out.println("\t\t\tBATCH SIZE : " + state.getBatchSize());
            System.out.println("\t\t\tBATCH UNIT : " + state.getBatchUnit());
            for (BOMEntryState entry : state.getEntries()) {
                System.out.println("\t\t++++++++++++++++++++++++++++++++++++++++"
                        + "\t\tBOM ENTRY ID   : " + entry.getId() + "\n"
                        + "\t\tMATERIAL ID    : " + entry.getMaterialId() + "\n"
                        + "\t\tQUANTITY       : " + entry.getQuantity() + "\n"
                        + "\t\tUNIT           : " + entry.getUnit() + " / batch \n");
                System.out.println("\t\t++++++++++++++++++++++++++++++++++++++++");
            }

        } catch (Exception e) {
            System.out.println("Create failed");
            e.printStackTrace();
        }

    }

    public void createProductBatch() {
        try {
            List<PlannedDemandState> states = dmb.viewPlannedDemands(3);

            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.YEAR, 2011);
            cal.set(Calendar.MONTH, 8);
            cal.set(Calendar.DAY_OF_MONTH, 1);

            Date startingDate = cal.getTime();

            for (PlannedDemandState state : states) {
                if (state.getExecDate().after(startingDate)) {
                    pmb.createProductBatch(state.getId(), "created");
                }
            }

            System.out.println("Product batch created");
        } catch (Exception e) {
            System.out.println("Create failed");
            e.printStackTrace();
        }
    }

    public void withdrawProductBatch() {
        try {
            System.out.print("Enter Batch ID         : ");
            Long batchId = Long.parseLong(scanner.nextLine());
            System.out.print("Reason for Withdrawal  : ");
            String remarks = scanner.nextLine();

            pmb.withdrawProductBatch(batchId, remarks);

            System.out.println("Product batch created");
        } catch (Exception e) {
            System.out.println("Create failed");
            e.printStackTrace();
        }
    }

    public void initializeUser() {
        System.out.println("Initializing users.");

        List<String> roles = new ArrayList<String>();
        roles.add("Customer Executive");
        rb.createNewUser("anindya.pradipta1004@gmail.com", "Anindya Pradipta", "123456", roles, "25 Prince George's Park", "91572517", "Female", "Jakarta", "Indonesia", "What is your surname?", "Pradipta");
        lb.activateAccount("anindya.pradipta1004@gmail.com");
        roles.removeAll(roles);

        roles.add("Finance Executive");
        rb.createNewUser("jessicamt91@gmail.com", "Jessica Maria Teguh", "123456", roles, "25 Prince George's Park", "92282310", "Female", "Singapore", "Singapore", "What is your surname?", "Teguh");
        lb.activateAccount("jessicamt91@gmail.com");
        roles.removeAll(roles);

        roles.add("Supply Chain Executive");
        roles.add("Production Executive");
        rb.createNewUser("ji.suryantana@gmail.com", "Indraperwira Suryantana", "123456", roles, "25 Prince George's Park", "92301417", "Male", "Jakarta", "Indonesia", "What is your surname?", "Suryantana");
        lb.activateAccount("ji.suryantana@gmail.com");
        roles.removeAll(roles);

        roles.add("President");
        rb.createNewUser("suatboontan@gmail.com", "Tan Suat Boon", "123456", roles, "25 Little India Road", "93288511", "Female", "Penang", "Malaysia", "What is your surname?", "Tan");
        lb.activateAccount("suatboontan@gmail.com");
        roles.removeAll(roles);

        roles.add("Customer Executive");
        roles.add("Finance Executive");
        roles.add("Technical Support Executive");
        rb.createNewUser("irvan.sudji@gmail.com", "Irvan Sudjianto", "123456", roles, "25 Prince George's Park", "93668093", "Male", "Surabaya", "Indonesia", "What is your surname?", "Sudjianto");
        lb.activateAccount("irvan.sudji@gmail.com");

        System.out.println("Initialization of users successful.");
    }

    public void addWarehouse() {

        try {

            System.out.print("Name              : ");
            String name = scanner.nextLine();
            System.out.print("Capacity          :");
            int capacity = Integer.parseInt(scanner.nextLine());
            System.out.print("Address(Street)           : ");
            String street = scanner.nextLine();
            System.out.print("City    : ");
            String city = scanner.nextLine();
            System.out.print("Country           : ");
            String country = scanner.nextLine();
            System.out.print("PostalCode              : ");
            String postcode = scanner.nextLine();

            Long warehouseId = iib.addWarehouse(name, capacity, street, city, country, postcode);
            System.out.println("Warehouse Id is : " + warehouseId);
        } catch (Exception ex) {
            System.out.println("add error");
            ex.printStackTrace();

        }
    }

    public void deleteWarehosue() {

        System.out.println("Warehouse Id : ");
        Long id = scanner.nextLong();

        iib.deleteWarehouse(id);
    }

    public void editWarehouse() {

        try {
            System.out.print("ID            :");
            Long id = Long.parseLong(scanner.nextLine());
            System.out.print("Name          : ");
            String name = scanner.nextLine();
            System.out.print("Street   : ");
            String street = scanner.nextLine();
            System.out.print("City    : ");
            String city = scanner.nextLine();
            System.out.print("Country      : ");
            String country = scanner.nextLine();
            System.out.print("Postal Code       : ");
            String postcode = scanner.nextLine();


            iib.editWarehouse(id, name, street, city, country, postcode);
            System.out.println("Warehouse detail edited!");
        } catch (Exception ex) {
            System.out.println("create error");
            ex.printStackTrace();
        }
    }

    public void viewWarehouse() {

        List<WarehouseState> stateList = new ArrayList();
        stateList = iib.viewWarehouse();
        System.out.println("\t\t\tWAREHOUSE LIST");
        System.out.println("***************************************************************");
        for (Object o : stateList) {
            WarehouseState wState = (WarehouseState) o;
            System.out.println("\t\tID             : " + wState.getId() + "\n"
                    + "\t\tNAME           : " + wState.getName() + "\n"
                    + "\t\tSTREET    : " + wState.getStreet() + "\n"
                    + "\t\tCITY : " + wState.getCity() + "\n"
                    + "\t\tCOUNTRY    : " + wState.getCountry() + "\n"
                    + "\t\tPOSAL CODE  : " + wState.getPostcode() + "\n");

            System.out.println("***************************************************************");
        }
    }

    public void testBreakdownPMD() {
        try {
            System.out.println("Testing PMD breakdown");

            dmb.breakdownMonthPlannedDemands(2011, 12);
            dmb.breakdownMonthPlannedDemands(2012, 1);
            dmb.breakdownMonthPlannedDemands(2012, 2);
            dmb.breakdownMonthPlannedDemands(2012, 3);

        } catch (Exception e) {
            System.out.println("Test failed: " + e.getMessage());
        }
    }
}
